	/**
 * 
 */
package ro.dta.idbi.patterns;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ro.dta.idbi.interfaces.IntegrationOperation;
import ro.dta.idbi.model.AbstractPattern;
import ro.dta.idbi.model.AbstractPrimitivePattern;
import uk.ac.ic.doc.automed.IntegrityException;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;

/**
 * <b>Reverse transformation for the mandatory column and total generalisation
 * transformation pattern</b><br />
 * 
 * Example of usage:<br />
 * 
 * <code>remove_col_generalisation(&lt;&lt;parentTable,col&gt;&gt;, &lt;&lt;newTable1&gt;&gt;,...,&lt;&lt;newTablen&gt;&gt;, &#123;val1,...,valn&#125;)</code>
 * <br />
 * 
 * @author Tudor Dobrila
 * 
 */
public class ColGenerRev extends AbstractPattern {

	private static final Logger LOGGER = Logger.getLogger("ro.dta.idbi.patterns.ColGenerRev");

	/**
	 * Name of the pattern
	 */
	public static final String NAME = "Remove Column Generalisation";

	/**
	 * Description of the pattern
	 */
	public static final String DESCRIPTION = "Reverse mandatory column and total generalisation equivalence";

	/**
	 * Name of the command associated with the pattern
	 */
	public static final String COMMAND = "remove_col_generalisation";

	/**
	 * Integration phase during which the command is applied
	 */
	public static final EnumSet<IntegrationOperation> OPERATION = EnumSet
			.of(IntegrationOperation.CONFORMING);

	/**
	 * Name of the column over which the transformation is applied
	 */
	private String columnName;

	/**
	 * Column values
	 */
	private List<String> colValues;

	/**
	 * Name of the new tables being created
	 */
	private List<String> tables;

	/**
	 * Reverse of column generalisation pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Source schema over which the transformation is applied
	 */
	public ColGenerRev(final Schema sourceSchema) throws NotFoundException, IntegrityException {
		super(sourceSchema);

		pattern = Pattern.compile("^" + COMMAND + "[ ]*\\((" + COL_REGEX + ")" + COMMA_REGEX
				+ TABLE_SEQ_REGEX + COMMA_REGEX + VAL_SEQ_REGEX + "\\)$");
	}

	/**
	 * Reverse of column generalisation pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Name of source schema over which the transformation is applied
	 */
	public ColGenerRev(final String sourceSchema) throws NotFoundException, IntegrityException {
		this(Schema.getSchema(sourceSchema));
	}
	
	@Override
	protected List<String> getPrimitiveCommands() throws NotFoundException, IntegrityException {
		ColGener forward = new ColGener(sourceSchema, columnName, colValues, tables);
		List<String> commands = forward.getPrimitiveCommands();
		List<String> result = new ArrayList<String>();
		for (String cmd : commands) {
			result.add(0, AbstractPrimitivePattern.getReverseTrans(cmd));
		}
		
		return result;
	}

	@Override
	protected boolean verify() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.model.AbstractPattern#parse(java.lang.String)
	 */
	@Override
	protected void parse(String trans) {
		Matcher matcher = pattern.matcher(trans);
		matcher.find();
		ColGener forward;
		try {
			forward = new ColGener(sourceSchema);
			forward.parse(matcher);
			columnName = forward.getColumnName();
			tables = new ArrayList<String>(forward.getTables());
			colValues = new ArrayList<String>(forward.getColValues());
		} catch (NotFoundException e) {
			LOGGER.log(Level.SEVERE, "Fatal exception while parsing command.", e);
		} catch (IntegrityException e) {
			LOGGER.log(Level.SEVERE, "Fatal exception while parsing command.", e);
		}
	}

}
